/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	BPromptMgr.java
 * 模块说明：	
 * 修改历史：
 * 2006-11-16 - lxm - 创建。
 */
package com.hd123.rumba.prompt.biz;

import java.util.ArrayList;
import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.type.Type;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.prompt.CPrompt;
import com.hd123.rumba.prompt.IPrompt;
import com.hd123.rumba.prompt.IPromptMgr;
import com.hd123.rumba.runtime.IManager;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.runtime.biz.BManager;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.util.ClassifiedHashtable;

/**
 * 提示信息 | 业务层 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class BPromptMgr extends BManager implements IBPromptMgr {

  @Override
  public IManager getPerzMgr() throws BusinessException {
    return getPromptMgr();
  }

  public void readPromptT(String promptUuid, OperateContext operate, long promptOca)
      throws BusinessException, HibernateException {
    this.checkParameterNotNull(promptUuid, "promptUuid");
    this.checkParameterNotNull(operate, "operate");

    IPrompt pp = getPromptMgr().getPrompt(promptUuid);
    ClassifiedHashtable h = this.checkOca(pp, promptOca, CPrompt.CAPTION_CLASS, pp.getMessage());
    if (h != null)
      throw new BusinessException(h.getAllMessages());

    pp.setState(CPrompt.VALUE_STATE_READ);

    Transaction tx = getSession().beginTransaction();
    try {
      getPromptMgr().save(getSession(), pp, operate);
      tx.commit();
    } catch (BusinessException e) {
      tx.rollback();
      throw e;
    } catch (HibernateException e) {
      tx.rollback();
      throw e;
    } finally {
      getSession().flush();
    }
  }

  public BPrompt getPromptBySender(String senderId) throws BusinessException, HibernateException {
    this.checkParameterNotNull(senderId, "senderId");
    List list = getSession().find(
        "from " + getPromptMgr().lookupClass(IPrompt.class) + " p" + " where p.senderId = ?",
        senderId, Hibernate.STRING);
    if (list.size() > 0) {
      BPrompt bp = new BPrompt();
      bp.readFrom((IPrompt) list.get(0));
      return bp;
    } else
      return null;
  }

  public List createPromptListByReceiver(IUser user) throws BusinessException, HibernateException {
    this.checkParameterNotNull(user, "User");
    List list = getSession()
        .find(
            "from "
                + getPromptMgr().lookupClass(IPrompt.class)
                + " p "
                + " where p.state = ? and (p.receiverUser.uuid = ?"
                + " or p.receiverRole.uuid in (select ur.role.uuid from UserRole ur where ur.user.uuid = ?))"
                + " order by p.level desc, p.lastModified asc", new Object[] {
                CPrompt.VALUE_STATE_NORMAL, user.getUuid(), user.getUuid() }, new Type[] {
                Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING });
    if (list != null && list.size() > 0) {
      return list;
    } else
      return new ArrayList();
  }

  private IPromptMgr promptMgr = null;

  private IPromptMgr getPromptMgr() throws BusinessException {
    if (promptMgr == null)
      promptMgr = (IPromptMgr) NamingMgr.getInstance().lookupMgr(IPromptMgr.class);
    return promptMgr;
  }

}
